package com.steven.im.ui.fragment.goodsdetail;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.flyco.tablayout.SlidingTabLayout;
import com.steven.putorefresh.BaseBothListener;
import com.steven.putorefresh.PullToBothLayout;
import com.steven.im.App;
import com.steven.im.R;
import com.steven.im.adapter.AppraiseAdapter;
import com.steven.im.base.ViewPagerFragment;
import com.steven.im.entity.BaseEntity;
import com.steven.im.net.MyCallback;
import com.steven.im.net.Task;
import com.steven.im.utils.ToastUtil;

import retrofit2.Response;

/**
 * Created by Steven on 2019/1/15.
 */
@SuppressLint("ValidFragment")
public class AppraiseFragment extends ViewPagerFragment {

    int topHeight = 0;//顶部的距离
    LinearLayout rootLayout;//跟布局
    private RelativeLayout headerRel;
    private ImageView backImg;
    private ImageView shareImg;
    private SlidingTabLayout slidingTabLayout;
    private JSONArray mList;//评价列表
    private JSONArray loadList;//加载_评价列表
    private ImageView backImg1;
    private ImageView shareImg1;
    private AppraiseAdapter adapter;
    private View view;
    private PullToBothLayout pullToBothLayout;
    private RecyclerView recyclerView;
    private String goodId;

    private int pageNo = 1;//默认是第一页
    private int pageSize = 10;//默认 加载条数
    private boolean isRefresh = false;//是否是下拉刷新
    private boolean isLoadMore = false;//是否是 上拉加载
    private AppraiseAdapter appraiseAdapter = null;


    private int pageCount = 1;//当前 页数
    private int totalPage = 1;//总页数

    public AppraiseFragment(String goodId, int titleHeight, RelativeLayout headerRel, SlidingTabLayout slidingTabLayout, ImageView backImg, ImageView shareImg, ImageView backImg1, ImageView shareImg1) {
        this.goodId = goodId;
        this.topHeight = titleHeight;
        this.headerRel = headerRel;
        this.slidingTabLayout = slidingTabLayout;
        this.backImg = backImg;
        this.shareImg = shareImg;
        this.backImg1 = backImg1;
        this.shareImg1 = shareImg1;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_appraise, null);
        pullToBothLayout = view.findViewById(R.id.pullRefreshBoth);
        recyclerView = view.findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //设置跟布局的padding值
        rootLayout = view.findViewById(R.id.ll_rootlayout);
        rootLayout.setPadding(0, topHeight, 0, 0);
    }


    @Override
    protected void onFirstUserVisible() {
        pageNo = 1;
        pageSize = 10;
        setVisible();
        getFeedBack();//获取商品的评价信息
    }

    @Override
    protected void onUserVisible() {
        setVisible();
    }

    private void setVisible() {
        //可见时，标题栏容器不透明,文字标题可见,按钮标题不可见
        headerRel.getBackground().setAlpha(255);
        headerRel.findViewById(R.id.rl_titlebar).setVisibility(View.VISIBLE);
        headerRel.findViewById(R.id.rl_iconbar).setVisibility(View.GONE);
        backImg.setImageAlpha(255);
        shareImg.setImageAlpha(255);
        slidingTabLayout.setTextSelectColor(getAlphaColorSelect());
        slidingTabLayout.setTextUnselectColor(getAlphaColorUnSelect());
        slidingTabLayout.setIndicatorColor(getAlphaIndicatorColorSelect());
        setListener();
    }

    @Override
    protected void onUserInvisible() {

    }

    //获取评价信息
    private void getFeedBack() {
        Task.getApiService().getFeedBack(pageNo + "", pageSize + "", goodId).enqueue(new MyCallback<BaseEntity>(getActivity()) {
            @Override
            public void onSuccess(Response<BaseEntity> response) {
                pullToBothLayout.finishRefresh();
                pullToBothLayout.finishLoadMore();
                if (("0").equals(response.body().getCode())) {
                    JSONObject object = ((JSONObject) (JSON.toJSON(response.body().getResult().get("pager"))));
                    pageCount = Integer.parseInt(object.getString("pageNo"));
                    totalPage = Integer.parseInt(object.getString("totalPage"));
                    if (isRefresh) {
                        isRefresh = false;
                        mList.clear();
                        mList.addAll(response.body().getResult().getJSONArray("feedbackList"));
                        appraiseAdapter.notifyDataSetChanged();
                    } else if (isLoadMore) {
                        isLoadMore = false;
                        loadList = response.body().getResult().getJSONArray("feedbackList");
                        mList.addAll(loadList);
                        appraiseAdapter.notifyDataSetChanged();
                    } else {
                        mList = response.body().getResult().getJSONArray("feedbackList");
                        if (appraiseAdapter == null) {
                            setData();
                        } else {
                            appraiseAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }

            @Override
            public void onFailure(Throwable t) {
                pullToBothLayout.finishRefresh();
                pullToBothLayout.finishLoadMore();
            }
        });
    }

    private void setListener() {
        pullToBothLayout.setRefreshListener(new BaseBothListener() {
            @Override
            public void refresh() {
                isRefresh = true;
                pageNo = 1;
                pageSize = 10;
                getFeedBack();
            }

            @Override
            public void loadMore() {
                if (pageCount < totalPage) {
                    isLoadMore = true;
                    pageNo++;
                    pageSize = 10;
                    getFeedBack();
                } else {
                    pullToBothLayout.finishLoadMore();
                    ToastUtil.showShort(App.getContext(), "没有更多数据了！");
                }
            }
        });
    }

    //初始化数据
    private void setData() {
        appraiseAdapter = new AppraiseAdapter(getActivity(), mList);
        recyclerView.setAdapter(appraiseAdapter);
    }

    private int getAlphaIndicatorColorSelect() {
        return Color.argb((int) (1.0f * 255), 0xFF, 0x66, 0x00);
    }

    private int getAlphaColorSelect() {
        return Color.argb((int) (1.0f * 255), 0x33, 0x33, 0x33);
    }

    private int getAlphaColorUnSelect() {
        return Color.argb((int) (1.0f * 255), 0x66, 0x66, 0x66);
    }
}
